﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.ApplicationPages;

namespace Com.CodePlex.SPC3.ShowHideFields.Layouts.Com.CodePlex.SPC3 {

    //C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\formEdt.aspx

    public partial class ShowHideFields : ListPageBase /*LayoutsPageBase*/ {

        protected SPSite spServer;
        protected SPWeb spWeb;
        protected SPList spList;
        protected SPFieldCollection spFields;
        protected SPField spField;
        protected SPBaseType iBaseType;
        protected String strDefaultViewUrl;
        protected String strHttpPath;
        protected String strNextUrl;
        protected String strOrderText;
        protected String strNameText;
        protected String strOrderDescriptionText;

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
           
            spServer = SPControl.GetContextSite(Context);
            spWeb = SPControl.GetContextWeb(Context);
            Microsoft.SharePoint.ApplicationPages.ListPageBase.ThrowIfNoListQueryString();
            spList = spWeb.Lists.GetList(new Guid(Request.QueryString.GetValues("List")[0]), true);
            spList.CheckPermissions(SPBasePermissions.ManageLists);
            spFields = spList.Fields;
            PageTarget = spList;
            iBaseType = spList.BaseType;
            strDefaultViewUrl = spList.DefaultViewUrl;
            strHttpPath = spWeb.Url + "/_vti_bin/owssvr.dll?CS=65001";
            String L_NextUrlList_Text = "/_layouts/ListEdit.aspx";
            String L_NextUrlSurvey_Text = "/_layouts/SurvEdit.aspx";
            String L_FieldOrder_Text = (string)(this.GetGlobalResourceObject("wss", "formedt_L_FieldOrder_Text"));
            String L_QuestionOrder_Text = (string)(this.GetGlobalResourceObject("wss", "formedt_L_QuestionOrder_Text"));
            String L_FieldName_Text = (string)(this.GetGlobalResourceObject("wss", "formedt_L_FieldName_Text"));
            String L_QuestionName_Text = (string)(this.GetGlobalResourceObject("wss", "formedt_L_QuestionName_Text"));
            String L_OrderFieldsDescription_Text = (string)(this.GetGlobalResourceObject("wss", "formedt_L_OrderFieldsDescription"));
            String L_OrderQuestionsDescription_Text = (string)(this.GetGlobalResourceObject("wss", "formedt_L_OrderQuestionsDesc"));
            switch (iBaseType) {
                case SPBaseType.Survey:
                    strNextUrl = L_NextUrlSurvey_Text;
                    strOrderText = L_QuestionOrder_Text;
                    strNameText = L_QuestionName_Text;
                    strOrderDescriptionText = L_OrderQuestionsDescription_Text;
                    break;
                default:
                    strNextUrl = L_NextUrlList_Text;
                    strOrderText = L_FieldOrder_Text;
                    strNameText = L_FieldName_Text;
                    strOrderDescriptionText = L_OrderFieldsDescription_Text;
                    break;
            }

		    for (int i = 0; i < spFields.Count; i++) {
			    spField = spFields[i];
			    string strTitleTmp = (string)(this.GetGlobalResourceObject("wss", "formedt_positionfromtop_title"));
			    strTitleTmp = string.Format(strTitleTmp,SPHttpUtility.HtmlEncode(spField.Title));

                HtmlTableRow tr = new HtmlTableRow();
                tr.VAlign = "Top";

                HtmlTableCell td = new HtmlTableCell();
                td.Attributes["class"] ="ms-authoringcontrols";
                td.Attributes["width"] ="85%";
                //td.NoWrap = true;
                td.InnerText = SPHttpUtility.HtmlEncode(spField.InternalName);
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                if (spField.ReadOnlyField) {
                    td.Controls.Add(new LiteralControl("ReadOnly"));
                }
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                if (spField.Hidden) {
                    td.Controls.Add(new LiteralControl("Hidden"));
                }
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                td.NoWrap = true;
                RadioButtonList list = new RadioButtonList();
                list.ID = "ShowInDisplayForm_" + spField.InternalName;
                list.Visible = !spField.Hidden;
                list.Items.Add(new ListItem("Not Set", "Null"));
                list.Items.Add(new ListItem("Yes", "True"));
                list.Items.Add(new ListItem("No", "False"));
                list.SelectedValue = spField.ShowInDisplayForm == null ? "Null" : (spField.ShowInDisplayForm.Value ? "True" : "False");
                td.Controls.Add(list);
                HiddenField hidden = new HiddenField();
                hidden.ID = "oShowInDisplayForm_" + spField.InternalName;
                hidden.Visible = !spField.Hidden;
                hidden.Value = spField.ShowInDisplayForm == null ? "Null" : (spField.ShowInDisplayForm.Value ? "True" : "False");
                td.Controls.Add(hidden);
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                td.NoWrap = true;
                list = new RadioButtonList();
                list.ID = "ShowInEditForm_" + spField.InternalName;
                list.Visible = !spField.Hidden && !spField.ReadOnlyField;
                list.Items.Add(new ListItem("Not Set", "Null"));
                list.Items.Add(new ListItem("Yes", "True"));
                list.Items.Add(new ListItem("No", "False"));
                list.SelectedValue = spField.ShowInEditForm == null ? "Null" : (spField.ShowInEditForm.Value ? "True" : "False");
                td.Controls.Add(list);
                hidden = new HiddenField();
                hidden.ID = "oShowInEditForm_" + spField.InternalName;
                hidden.Visible = !spField.Hidden && !spField.ReadOnlyField;
                hidden.Value = spField.ShowInEditForm == null ? "Null" : (spField.ShowInEditForm.Value ? "True" : "False");
                td.Controls.Add(hidden);
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                td.NoWrap = true;
                list = new RadioButtonList();
                list.ID = "ShowInListSettings_" + spField.InternalName;
                list.Visible = !spField.Hidden;
                list.Items.Add(new ListItem("Not Set", "Null"));
                list.Items.Add(new ListItem("Yes", "True"));
                list.Items.Add(new ListItem("No", "False"));
                list.SelectedValue = spField.ShowInListSettings == null ? "Null" : (spField.ShowInListSettings.Value ? "True" : "False");
                td.Controls.Add(list);
                hidden = new HiddenField();
                hidden.ID = "oShowInListSettings_" + spField.InternalName;
                hidden.Visible = !spField.Hidden;
                hidden.Value = spField.ShowInListSettings == null ? "Null" : (spField.ShowInListSettings.Value ? "True" : "False");
                td.Controls.Add(hidden);
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                td.NoWrap = true;
                list = new RadioButtonList();
                list.ID = "ShowInNewForm_" + spField.InternalName;
                list.Visible = !spField.Hidden && !spField.ReadOnlyField;
                list.Items.Add(new ListItem("Not Set", "Null"));
                list.Items.Add(new ListItem("Yes", "True"));
                list.Items.Add(new ListItem("No", "False"));
                list.SelectedValue = spField.ShowInNewForm == null ? "Null" : (spField.ShowInNewForm.Value ? "True" : "False");
                td.Controls.Add(list);
                hidden = new HiddenField();
                hidden.ID = "oShowInNewForm_" + spField.InternalName;
                hidden.Visible = !spField.Hidden && !spField.ReadOnlyField;
                hidden.Value = spField.ShowInNewForm == null ? "Null" : (spField.ShowInNewForm.Value ? "True" : "False");
                td.Controls.Add(hidden);
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                td.NoWrap = true;
                td.Align = "Center";
                CheckBox check = new CheckBox();
                check.ID = "ShowInVersionHistory_" + spField.InternalName;
                check.Visible = !spField.Hidden;
                check.Checked = spField.ShowInVersionHistory;
                td.Controls.Add(check);
                hidden = new HiddenField();
                hidden.ID = "oShowInVersionHistory_" + spField.InternalName;
                hidden.Visible = !spField.Hidden;
                hidden.Value = spField.ShowInVersionHistory ? "on" : "";
                td.Controls.Add(hidden);
                tr.Cells.Add(td);

                td = new HtmlTableCell();
                td.NoWrap = true;
                list = new RadioButtonList();
                list.ID = "ShowInViewForms_" + spField.InternalName;
                list.Visible = !spField.Hidden;
                list.Items.Add(new ListItem("Not Set", "Null"));
                list.Items.Add(new ListItem("Yes", "True"));
                list.Items.Add(new ListItem("No", "False"));
                list.SelectedValue = spField.ShowInViewForms == null ? "Null" : (spField.ShowInViewForms.Value ? "True" : "False");
                td.Controls.Add(list);
                hidden = new HiddenField();
                hidden.ID = "oShowInViewForms_" + spField.InternalName;
                hidden.Visible = !spField.Hidden;
                hidden.Value = spField.ShowInViewForms == null ? "Null" : (spField.ShowInViewForms.Value ? "True" : "False");
                td.Controls.Add(hidden);
                tr.Cells.Add(td);

                phShowHideFields.Controls.Add(tr);

                tr = new HtmlTableRow();
                td = new HtmlTableCell();
                td.ColSpan = 9;
                HtmlGenericControl hr = new HtmlGenericControl("hr");
                td.Controls.Add(hr);
                tr.Cells.Add(td);
                phShowHideFields.Controls.Add(tr);
		    }
        }

        protected void BtnSave_Click(object sender, EventArgs e) {
            foreach(string okey in Page.Request.Form.Keys) {
                if (okey.Contains("oShowIn")) { //always use okey, as checkboxs dont submit if empty
                    string key = okey.Replace("oShowIn", "ShowIn");
                    if ((Request.Form[okey] + "") != (Request.Form[key] + "")) {
                        //Response.Write("Changed: " + key + "[" + Request.Form[okey] + "-->" + Request.Form[key] + "]<br/>");

                        string type = key.Substring(key.LastIndexOf("$") + 1);
                        string internalname = type.Substring(type.IndexOf("_") + 1);
                        type = type.Substring(0, type.IndexOf("_"));
                        spField = spFields.GetFieldByInternalName(internalname);
                        bool? value = null;
                        if (Request.Form[key] == "True" || Request.Form[key] == "on") {
                            value = true;
                        }
                        if (Request.Form[key] == "False" || (Request.Form[key]+"") == "") {
                            value = false;
                        }

                        switch (type) {
                            case "ShowInDisplayForm":
                                spField.ShowInDisplayForm = value;
                                break;
                            case "ShowInEditForm":
                                spField.ShowInEditForm = value;
                                break;
                            case "ShowInListSettings":
                                spField.ShowInListSettings = value;
                                break;
                            case "ShowInNewForm":
                                spField.ShowInNewForm = value;
                                break;
                            case "ShowInVersionHistory":
                                spField.ShowInVersionHistory = value.Value;
                                break;
                            case "ShowInViewForms":
                                spField.ShowInViewForms = value;
                                break;
                        }

                        spField.Update();
                    }
                }
            }
            SPUtility.Redirect(base.ListEditPageLayoutsRelativeUrl, SPRedirectFlags.RelativeToLayoutsPage, this.Context);
        }
    }
}
